package admin.forms;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;


import entities.ApplicationTemplate;

public class ApplicationTemplateForm {
	
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, ApplicationTemplate template) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String appId = (String) request.getAttribute("appId");
		String title = (String) request.getParameter("title");
		String description = (String) request.getParameter("description");
		String templateid = (String) request.getParameter("templateid");
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not empty.");
			template.setTitle("");
		} else {
			template.setTitle(title);
		}
		
		if (description == null || description.isEmpty()) {
			check.put("description", "Title is not empty.");
			template.setDescription("");
		} else {
			template.setDescription(description);
		}
		
		if (templateid == null || templateid.isEmpty()) {
			check.put("templateid", "Please choise a template");
			template.setId("");
		} else {
			template.setId(templateid);
		}
		
		form.put("checkValid", check);
		form.put("template", template);
		
		return form;
	}
	
	public static TreeMap<String, Object> checkEditValid(HttpServletRequest request, ApplicationTemplate template) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		String appId = (String) request.getAttribute("appId");
		String title = (String) request.getParameter("title");
		String description = (String) request.getParameter("description");
		String templateid = (String) request.getParameter("templateid");
		String header = (String) request.getParameter("header");
		String footer = (String) request.getParameter("footer");
		String home = (String) request.getParameter("home");
		String archives = (String) request.getParameter("archives");
		String articleInfo = (String) request.getParameter("articleInfo");
		String authorArticle = (String) request.getParameter("authorArticle");
		String authors = (String) request.getParameter("authors");
		String category = (String) request.getParameter("category");
		String tag = (String) request.getParameter("tag");
		String contact = (String) request.getParameter("contact");
		String rewriter = (String) request.getParameter("rewriter");
		String staticPage = (String) request.getParameter("staticPage");
		String submitArticle = (String) request.getParameter("submitArticle");
		String library = (String) request.getParameter("library");
		
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not empty.");
			template.setTitle("");
		} else {
			template.setTitle(title);
		}
		
		if (description == null || description.isEmpty()) {
			check.put("description", "Title is not empty.");
			template.setDescription("");
		} else {
			template.setDescription(description);
		}
		
		if (header == null || header.isEmpty()) {
			check.put("header", "Header is not empty");
			template.setHeader("");
		} else {
			template.setHeader(header);
		}
		
		if (footer == null || footer.isEmpty()) {
			check.put("footer", "Footer is not empty");
			template.setFooter("");
		} else {
			template.setFooter(footer);
		}
		
		if (home == null || home.isEmpty()) {
			check.put("home", "Home is not empty");
			template.setHome("");
		} else {
			template.setHome(home);
		}
		
		if (archives == null || archives.isEmpty()) {
			check.put("archives", "Archives is not empty");
			template.setArchives("");
		} else {
			template.setArchives(archives);
		}
		
		if (articleInfo == null || articleInfo.isEmpty()) {
			check.put("articleInfo", "Article iInfo is not empty");
			template.setArticleInfo("");
		} else {
			template.setArticleInfo(articleInfo);
		}
		
		if (authorArticle == null || authorArticle.isEmpty()) {
			check.put("authorArticle", "Author Article is not empty");
			template.setAuthorArticle("");
		} else {
			template.setAuthorArticle(authorArticle);
		}
		
		if (authors == null || authors.isEmpty()) {
			check.put("authors", "Authors is not empty");
			template.setAuthorArticle("");
		} else {
			template.setAuthorArticle(authorArticle);
		}
		
		if (category == null || category.isEmpty()) {
			check.put("category", "Category is not empty");
			template.setCategory("");
		} else {
			template.setCategory(category);
		}
		
		if (tag == null || tag.isEmpty()) {
			check.put("tag", "Tag is not empty");
			template.setTag("");
		} else {
			template.setTag("");
		}
		
		if (contact == null || contact.isEmpty()) {
			check.put("contact", "Contact is not empty");
			template.setCategory("");
		} else {
			template.setCategory(category);
		}
		
		if (rewriter == null || rewriter.isEmpty()) {
			check.put("rewriter", "Rewriter is not empty");
			template.setRewriter("");
		} else {
			template.setRewriter(rewriter);
		}
		
		if (staticPage == null || staticPage.isEmpty()) {
			check.put("staticPage", "Static Page is not empty");
			template.setStaticPage("");
		} else {
			template.setStaticPage(staticPage);
		}
		
		if (library != null) {
			template.setLibrary(library);
		}
		
		form.put("checkValid", check);
		form.put("template", template);
		
		return form;
	}
}
